package com.examstack.portal.controller.action;

import java.util.List;
import com.examstack.common.domain.question.CheckRecord;
import com.examstack.common.domain.question.Question;
import com.examstack.common.domain.question.QuestionStatistic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.examstack.common.domain.exam.Message;
import com.examstack.common.domain.exam.UserQuestionHistory;
import com.examstack.common.domain.question.QuestionHistory;
import com.examstack.portal.security.UserInfo;
import com.examstack.portal.service.QuestionHistoryService;
import com.examstack.portal.service.QuestionService;

import java.util.Date;

@Controller
public class PracticeAction {
	
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionHistoryService questionHistoryService;
	/**
	 * 练习模式完成一道题
	 * 
	 * @param qh
	 * @return
	 */
	@RequestMapping(value = "/student/practice-improve", method = RequestMethod.POST)
	public @ResponseBody Message submitPractice(@RequestBody QuestionHistory qh) {
		Message msg = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		
		UserQuestionHistory history = new UserQuestionHistory();
		history.setQuestionId(qh.getQuestionId());
		history.setUserId(userInfo.getUserid());
		history.setQuestionTypeId(qh.getQuestionTypeId());
		boolean isRight = qh.getAnswer().equals(qh.getMyAnswer()) ? true : false;
		history.setRight(isRight);
		
		try {
			questionHistoryService.addUserQuestionHist(history);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg.setResult(e.getClass().getName());
			e.printStackTrace();
		}

		return msg;
	}
	/**
	 * 有异议试题
	 */
	@RequestMapping(value = "/student/question/sign-question/{questionId}-{dissent}", method = RequestMethod.GET)
	public @ResponseBody
	Message updateQuestion(Model model, @PathVariable("questionId") int questionId
			, @PathVariable("dissent") int dissent) {

		// UserDetails userDetails = (UserDetails)
		// SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Message message = new Message();
		Question question = new Question();
		CheckRecord checkRecord = new CheckRecord();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			//获取已开放试题数目
			List<QuestionStatistic> releasedQuestion = questionService.getSignQuestion(10,1);
			//获取有异议试题数目
			List<QuestionStatistic> signQuestion = questionService.getSignQuestion(dissent,1);
			int amountSign = signQuestion.get(0).getAmount();
			int amountReleased = releasedQuestion.get(0).getAmount();
			int a = amountReleased/20;
			if(amountSign > a){
				message.setResult("超出标记数量");
				return message;
			}
			/*if(status<1||status>8){
				message.setResult("试题状态错误！");
				return message;
			}*/
			question.setId(questionId);
			question.setDissent(dissent);
			question.setIs_visible(true);
			checkRecord.setCheckMan(userInfo.getUserid());
			checkRecord.setQuestionId(questionId);
			checkRecord.setOldStatus(10);
			checkRecord.setNewStatus(dissent);
			checkRecord.setCheckTime(new Date());

			questionService.updateQuestionStatus(question);
			questionService.addCheckRecord(checkRecord);

		} catch (Exception ex) {
			message.setResult(ex.getClass().getName());
		}
		return message;
	}
}
